package com.beautiful.xin.sort;

/**
 * @author xuqingxin 2021/8/9 12:06 下午
 */
public class ZigZag {
    public static void main(String[] args) {

        int[][] a = {{1, 2, 3}, {4, 5, 6}};
        zigzag(a);
        System.out.println("=====");
        zigzagg(a);
    }

    public static void zigzag(int[][] a) {
        int i = 0, j = 0;
        int r = a.length;
        int c = a[0].length;
        int n = r + c - 1;
        for (int k = 0; k < n; k++) {
            if (k % 2 == 0) {
                while (i >= 0 && j < c) {
                    System.out.println(a[i][j]);
                    i--;
                    j++;
                }
                i++;
                if (j >= c) {
                    j--;
                    i++;
                }
            } else {
                while (j >= 0 && i < r) {
                    System.out.println(a[i][j]);
                    j--;
                    i++;
                }
                j++;
                if (i >= r) {
                    i--;
                    j++;
                }
            }
        }
    }


    public static void zigzagg(int[][] a) {
        int i = 0, j = 0;
        int r = a.length;
        int c = a[0].length;
        int n = r + c - 1;
        for (int k = 0; k < n; k++) {
            if (k % 2 == 0) {
                while (i >= 0 && j < c) {
                    System.out.println(a[i][j]);
                    i--;
                    j++;
                }
                i++;
                if(j>=c){
                    j--;
                    i++;
                }
            }else{
                while (j >= 0 && i < r) {
                    System.out.println(a[i][j]);
                    j--;
                    i++;
                }
                j++;
                if(i>=r){
                    i--;
                    j++;
                }
            }
        }
    }


}
